// index.js

const util = require("../../utils/util");

Page({
  data: {
      id: '',
      name: '',
      mobile: '',
      address: '',
      total: 0,
      uid: 0,
      count: 0,
      pid: 0,
      single_cost: '',
  },
  bindNameInput(e) {
    this.setData({
      name: e.detail.value
    });
  },
  bindNumberInput(e) {
    this.setData({
      count: e.detail.value,
      total: e.detail.value*this.data.single_cost,
    });
  },
  bindMobileInput(e) {
    this.setData({
      mobile: e.detail.value
    });
  },
  
  bindAddressInput(e) {
    this.setData({
      address: e.detail.value
    });
  },
  
  submitForm() {
    var date = new Date();
    var month = (date.getMonth() + 1).toString().padStart(2, '0');
    var day = date.getDate().toString().padStart(2, '0');
    var hours = date.getHours().toString().padStart(2, '0');
    var minutes = date.getMinutes().toString().padStart(2, '0');
    var seconds = date.getSeconds().toString().padStart(2, '0');
    
    var currentTime =  month + day + hours + minutes + seconds;

    console.log(this.data.id)
    console.log('姓名：', this.data.name);
    console.log('联系方式：', this.data.mobile);
    console.log('地址：', this.data.address);
    console.log('数量：', this.data.count);
    console.log('金额：', this.data.total)
    if(this.data.name ==="" || this.data.mobile==="" || this.data.address==="" || this.data.count===""){
      util.alert("请完善信息！")
      return false
    }else if(this.data.mobile.length !== 11){
      util.alert("请输入正确的手机号")
      return false
    }else if(this.data.count <= 0){
      util.alert("商品数量不合法")
      return false
    }
    this.data.uid = wx.getStorageSync('uid')
    
    var item = wx.getStorageSync('商品信息')
    console.log("查看商品信息内容")
    console.log(item)
    this.data.pid = item.id
    this.data.id = currentTime;
    wx.setStorageSync('订单信息', this.data)
    console.log("订单信息向后面传送")
    console.log(this.data)
    util.redirect('confirmcommodity')
  },
  onLoad(options){
    
    this.data.single_cost = wx.getStorageSync('商品单价')
    this.setData(this.data)
  }
})